﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.UI;

namespace MVC.Web.UI
{
    public abstract class MvcControl
    {

        #region Fields

        private System.Web.UI.AttributeCollection _attributes;
        private string _tagName;
        private ViewContext _context;
        private int _width = 400;
        private int _height = 200;

        #endregion

        #region Constructors
        
        protected MvcControl(string tag, ViewContext context)
        {
            this._tagName = tag;
            this._context = context;
        }

        protected MvcControl()
        {
        }
        
        #endregion

        #region Properties
        
        public int Width 
        { 
            get {return _width;}
            set 
            {
                this._width = value;
            } 
        }

        public int Height
        {
            get {return _height;}
            set 
            {
                this._height = value;
            } 
        }
        
        public string Name { get; set; }

        public string  Scripts { get; set; }

        public System.Web.UI.AttributeCollection Attributes
        {
            get
            {
                if (_attributes == null)
                {
                    System.Web.UI.StateBag bag = new System.Web.UI.StateBag(true);
                    _attributes = new System.Web.UI.AttributeCollection(bag);
                }
                return _attributes;
            }
        }

        protected string TagName
        {
            get { return _tagName; }

            set { _tagName = value; }
        }
      
        #endregion


        
        public  virtual  void RenderControl(ViewContext context)
        { 
                using (System.Web.UI.HtmlTextWriter writer = new System.Web.UI.HtmlTextWriter(context.Writer))
                {
                    writer.AddAttribute("id", this.Attributes["id"]);
                    //writer.AddAttribute("style", "width:" + Width+"px");
                    //writer.AddAttribute("style", "height:" + Height + "px; width:"+ Width+ "px");
                    writer.RenderBeginTag(this._tagName);
                    this.RenderContents(writer);
                    writer.RenderEndTag();
                    this.RenderScripts(writer);
                }
         }
           
        protected abstract void RenderContents(HtmlTextWriter writer);
         
        protected abstract void RenderScripts(HtmlTextWriter writer); 
    }
    
}
